<template>
  <div class="article-select-view">
    <div class="header"></div>
    <div class="title">
      <div class="title-text">
        {{ select }}
      </div>
    </div>
    <div class="content">
      <ArticleDisplay :display-articles="displayArticles"/>
    </div>
  </div>
  <Catalogue/>
</template>

<script setup>
import ArticleDisplay from '../components/ArticleDisplay.vue';
import Catalogue from '../components/Catalogue.vue';
import { ref, watch, inject } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getArticle_type, getArticle_tag, getArticle_serach } from '../api/api'
import { ElNotification } from 'element-plus';

// type或tag名字
const select = ref()
// 展示文章list
const displayArticles = ref([])

// 获取路由参数 type or tag
const route = useRoute()
const router = useRouter()
const type = ref(route.query?.type)
const tag = ref(route.query?.tag)
const search = ref(route.query?.search)

// type和tag优先type 
// 然后是search
if (type.value) {
  // 读取type参数
  select.value = type.value
  getArticle_type(type.value).then(response => {
    if (response.data.length > 0) {
      displayArticles.value = response.data
    } else {
      ElNotification({
        title: '获取文章失败',
        message: '没有该分类',
        type: 'error'
      })
    }
  }).catch(error => {
    console.log(error)
    router.push({name: 'home'})
    ElNotification({
      title: '获取文章失败',
      message: error.message,
      type: 'error'
    })
  })
} else if (tag.value) {
  // 读取tag参数
  select.value = '< ' + tag.value + ' >'
  getArticle_tag(tag.value).then(response => {
    if (response.data.length > 0) {
      displayArticles.value = response.data
    } else {
      ElNotification({
        title: '获取文章失败',
        message: '没有该tag',
        type: 'error'
      })
    }
  }).catch(error => {
    console.log(error)
    router.push({name: 'home'})
    ElNotification({
      title: '获取文章失败',
      message: error.message,
      type: 'error'
    })
  })
} else if (search.value) {
  // 读取搜索关键字
  select.value = '[ ' + search.value + ' ]'
  getArticle_serach(search.value).then(response => {
    if (response.data.length > 0) {
      displayArticles.value = response.data
    } else {
      ElNotification({
        title: '搜索失败',
        message: '没有文章哦',
        type: 'error'
      })
    }
  }).catch(error => {
    console.log(error)
    router.push({name: 'home'})
    ElNotification({
      title: '获取文章失败',
      message: error.message,
      type: 'error'
    })
  })
} else {
  // 没有参数
  router.push({name: 'home'})
  ElNotification({
    title: '404 Not Found',
    message: '获取文章失败',
    type: 'error'
  })
}

// reload页面 从app.vue调用的方法
const reload = inject('Reload')
// 监听路由变化刷新页面
watch(() => route.path, (toPath) => {
  reload()
},{deep: true}) // 必须是deep，不然监听不到query

// 初始化回到页面顶部
document.documentElement.scrollTop = 0
document.body.scrollTop = 0
window.scrollX = 0
</script>

<style scoped>
.article-select-view {
  height: fit-content;
  min-height: 500px;
  width: 100%;
  background-color: #F5F7FA;
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: center;
}
.article-select-view .header {
  height: 4.5rem;
  width: 100%;
  background-color: #4dcdef;
}
.article-select-view .title {
  height: 8rem;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
}
.title-text {
  height: 4rem;
  width: 40rem;
  background-color: #fff;
  box-shadow: var(--el-box-shadow);
  border-radius: .25rem;
  text-align: center;
  line-height: 4rem;
  font-size: 2.5rem;
  color: #4dcdef;
  font-weight: 300;
}
.article-select-view .content {
  height: fit-content;
  width: 100%;
  padding: 0 5rem;
}
</style>